package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySupplierAuthBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.utils.GlideEngine;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.AuthEvent;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.viewmodel.self.SupplierAuthModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class SupplierAuthActivity extends AppActivity {
    private ActivitySupplierAuthBinding binding;
    private final int LICENS = 10;//营业执照
    private final int LETTER_OF_ATTORNEY = 11;//授权委托书
    private int type;
    @Inject
    SupplierAuthModel viewModel;
    private List<String> listLicens;
    private List<String> listLetter;
    private Intent intent;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SupplierAuthActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        listLicens = new ArrayList<>();
        listLetter = new ArrayList<>();
        binding = DataBindingUtil.setContentView(SupplierAuthActivity.this, R.layout.activity_supplier_auth);
        binding.setViewModel(viewModel);
        binding.include.actionBarTitle.setText("企业认证");
        binding.include.actionBarImageLeft.setVisibility(View.VISIBLE);
        binding.include.actionBarImageLeft.setOnClickListener(this);
        binding.nextView.setOnClickListener(this);
        binding.uploadLl.setOnClickListener(this);
        binding.displayLicenseImg.setOnClickListener(this);
//        binding.uploadLetterOfAttorneyLl.setOnClickListener(this);
//        binding.displayLetterImg.setOnClickListener(this);
    }

    @Override
    protected void init() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                finish();
                break;
            case R.id.next_view:
                //判断是否有图片
                if (listLicens.size() != 0) {
                    EventBus.getDefault().postSticky(AuthEvent.make().setMessageTag(EventConstant.EVENT_SELF_INVOICE_INFO).setMessageData(viewModel.supplierAuthEntity.get()));
                    InvoiceAuthActivity.start(SupplierAuthActivity.this);
                } else {
                    MToast.showToast(SupplierAuthActivity.this, "请先上传资料、照片");
                }
                break;
            case R.id.upload_ll:
                openAlbum(LICENS);
                break;
//            case R.id.upload_letter_of_attorney_ll:
//                openAlbum(LETTER_OF_ATTORNEY);
//                break;
//            case R.id.display_letter_img:
//                openPhotoView(listLetter);
//                break;
            case R.id.display_license_img:
                openPhotoView(listLicens);
                break;
        }
    }

    //打开相册
    private void openAlbum(int type) {
        this.type = type;
        PictureSelector.create(SupplierAuthActivity.this)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.SINGLE)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public void removeList() {
        listLicens.clear();
        binding.displayLicenseImg.setVisibility(View.GONE);
    }

    //预览
    private void openPhotoView(List<String> urls) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setClass(SupplierAuthActivity.this, PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, 0);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    if (type == LICENS) {
                        // 图片结果回调
                        List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                        File file = new File(selectList.get(0).getRealPath());
                        MLog.d("图片路径：", selectList.get(0).getRealPath());
                        if (file.exists()) {
                            MLog.d("图片", "存在");
                        }
                        listLicens.clear();
                        listLicens.add(selectList.get(0).getRealPath());
                        Glide.with(SupplierAuthActivity.this).load(selectList.get(0).getRealPath()).into(binding.displayLicenseImg);
                        binding.displayLicenseImg.setVisibility(View.VISIBLE);
                        viewModel.upLicenseModel(file);
                    } else if (type == LETTER_OF_ATTORNEY) {
                        List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                        File file = new File(selectList.get(0).getRealPath());
                        MLog.d("图片路径：", selectList.get(0).getRealPath());
                        if (file.exists()) {
                            MLog.d("图片", "存在");
                        }
                        listLetter.clear();
                        listLetter.add(selectList.get(0).getRealPath());
                        viewModel.upEntrustModel(file);
                    }

                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getRealPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    break;
            }
        }
    }
}
